﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace ClientLocker.Utility
{
    public class ScreenBlocker
    {
        private static FCHook g_hookKeyLL;
        

        static ScreenBlocker()
        {
            g_hookKeyLL = new FCHook(HookType.WH_KEYBOARD_LL);
        }

        /// <summary>
        /// 锁屏
        /// </summary>
        /// <param name="pLockForm"></param>
        public static void LockScreen(Form pLockForm)
        {
            HideTaskManager(true);
            ShowTaskBar(false); //暂时不必隐藏任务栏，因为窗体已经遮住其。
            pLockForm.FormClosing += new FormClosingEventHandler(BlockClosing_Handle);
            g_hookKeyLL.HookInvoked += HookInvoked;
            g_hookKeyLL.Install();
        }

        /// <summary>
        /// 钩子在处理键盘事件之前调用的delegate所绑定的函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void HookInvoked(object sender, HookEventArgs e)
        {
            FCHook h = sender as FCHook;
            if (h != null)
            {
                h.CallNextProc = false;
            }
        }

        /// <summary>
        /// 解屏
        /// </summary>
        /// <param name="pLockForm"></param>
        public static void ReleaseScreen(Form pLockForm)
        {
            HideTaskManager(false);
            ShowTaskBar(true);
            pLockForm.FormClosing -= BlockClosing_Handle;
            pLockForm.Close();
            g_hookKeyLL.HookInvoked -= HookInvoked;
        }

        /// <summary>
        /// 阻止任务管理器弹出（防止通过其关闭窗体）
        /// </summary>
        private static void HideTaskManager(bool pIsHidden)
        {
            Process p = null;
            if (Process.GetProcessesByName("taskmgr").Count() > 0)
            {
                p = Process.GetProcessesByName("taskmgr")[0];
                p.Kill();
            }
            // 如果该操作为隐藏任务栏
            if (pIsHidden)
            {
                p = new Process();
                p.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
                p.StartInfo.FileName = "taskmgr.exe";
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.Start();
            }
        }

        private const int SW_HIDE = 0;//API参数表示隐藏窗口 
        private const int SW_SHOW = 5;//API参数表示用当前的大小和位置显示窗口 

        [DllImportAttribute("user32.dll")]
        private static extern int FindWindow(string ClassName, string WindowName);

        [DllImport("user32.dll")]
        private static extern int ShowWindow(int handle, int cmdShow);
        /// <summary>
        /// 控制任务栏显示
        /// </summary>
        /// <param name="pIsShow"></param>
        private static void ShowTaskBar(bool pIsShow)
        {
            int showFlag = pIsShow ? SW_SHOW : SW_HIDE;
            ShowWindow(FindWindow("Shell_TrayWnd", null), showFlag);
        }

        /// <summary>
        /// 屏蔽除了任务管理关闭窗体意外地所有关闭方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void BlockClosing_Handle(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
